package com.tlcx.kfip.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;


/**
 * @project KukerIL
 * @auther Kyle.DY
 * @date 2016/3/24 16:11
 * @qq 1045127238
 * @describe 测量工具类
 */
public class MeasureUtil {
    private static final String TAG = MeasureUtil.class.getSimpleName();

    private int width, height, densityDpi;
    private float density;

    private int topHeight;

    private Context mContext;

    private static MeasureUtil instance = null;

    private MeasureUtil(Context context) {
        mContext = context;
        initScreenWH();
    }

    public static void initialize(Context context) {
        if (instance == null) {
            instance = new MeasureUtil(context);
        }
    }

    public static MeasureUtil getInstance() {
//        if (instance == null) new NullPointerException(TAG + " MeasureUtil is not initialize.");
        return instance;
    }

    public void release() {
        mContext = null;
    }

    private void initScreenWH() {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        setWidth(dm.widthPixels);
        setHeight(dm.heightPixels);
        setDensity(dm.density);
        setDensityDpi(dm.densityDpi);

        int result = 0;
        int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = mContext.getResources().getDimensionPixelSize(resourceId);
        }
        setTopHeight(result);

    }


    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int sp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / scale + 0.5f);
    }

    public int getWidth() {
        return width;
    }

    private void setWidth(int width) {
        this.width = width;
        Log.i(TAG, "width= " + width);
    }

    public int getHeight() {
        return height;
    }

    private void setHeight(int height) {
        this.height = height;
        Log.i(TAG, "height= " + height);
    }

    public int getDensityDpi() {
        return densityDpi;
    }

    private void setDensityDpi(int densityDpi) {
        this.densityDpi = densityDpi;
        Log.i(TAG, "densityDpi= " + densityDpi);
    }

    public float getDensity() {
        return density;
    }

    private void setDensity(float density) {
        this.density = density;
        Log.i(TAG, "density= " + density);
    }

    public int getTopHeight() {
        return topHeight;
    }

    private void setTopHeight(int topHeight) {
        this.topHeight = topHeight;
        Log.i(TAG, "topHeight= " + topHeight);
    }
}
